/*
 * Created on Apr 25, 2005
 *
 */
package org.mushroomdb.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * @author Matias
 *
 */
public class AbstractByteArrayOutputStream extends OutputStream {
	private byte[] buffer;
	private ByteArrayOutputStream byteArrayOutputStream;

	/**
	 * Construye un nuevo AbstractByteArrayOutputStream.
	 */
	public AbstractByteArrayOutputStream(byte[] buffer) {
		this.byteArrayOutputStream = new ByteArrayOutputStream(buffer.length);
		this.buffer = buffer;
	}

	/**
	 * 
	 */
	public void close() throws IOException {
		byte[] written = this.byteArrayOutputStream.toByteArray();
		for (int i=0; i < this.buffer.length && i < written.length; i++) {
			this.buffer[i] = written[i];
		}
		this.byteArrayOutputStream.close();
	}
	
	/**
	 * 
	 */
	public void flush() throws IOException {
		this.byteArrayOutputStream.flush();
	}
	
	/**
	 * 
	 */
	public void write(byte[] arg0, int arg1, int arg2) throws IOException {
		this.byteArrayOutputStream.write(arg0, arg1, arg2);
	}
	
	/**
	 * 
	 */
	public void write(byte[] arg0) throws IOException {
		this.byteArrayOutputStream.write(arg0);
	}
	
	/**
	 * @see java.io.OutputStream#write(int)
	 */
	public void write(int arg0) throws IOException {
		this.byteArrayOutputStream.write(arg0);
	}

}
